﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Atom.Tool
{
    public class ConcurrentInteger
    {
        private int value;
        private Mutex tex = new Mutex();

        public ConcurrentInteger() { }

        public ConcurrentInteger(int value) { this.value = value; }

        public int GetAndAdd()
        {
            lock(this)
            {
                tex.WaitOne();
                value++;
                tex.ReleaseMutex();
                return value;
            }
        }

        public int GetAndReduce()
        {
            lock(this)
            {
                tex.WaitOne();
                value--;
                tex.ReleaseMutex();
                return value;
            }
        }
    }
}
